package com.module.ad_gromore.ad;

import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

import com.module.ad_gromore.LogADUtils;
import com.module.ad_gromore.VideoAdListener;
import com.module.ad_gromore.manager.AdFullVideoManager;
import com.bytedance.msdk.adapter.util.Logger;
import com.bytedance.msdk.api.AdError;
import com.bytedance.msdk.api.reward.RewardItem;
import com.bytedance.msdk.api.v2.GMAdConstant;
import com.bytedance.msdk.api.v2.ad.fullvideo.GMFullVideoAdListener;
import com.bytedance.msdk.api.v2.ad.fullvideo.GMFullVideoAdLoadCallback;

import java.util.Map;

/**
 * 全屏视频
 * 好像取消了，使用插屏的，，插全屏
 */
@Deprecated
public class FullVideoAd {
    private AdFullVideoManager mAdFullVideoManager;
    private boolean mLoadSuccess = false;
    private final String TAG = "广告";
    private VideoAdListener listener;
    private String adUnitId;

    private static FullVideoAd videoAd;

    public static void loadAd(Activity context, final String adUnitId, VideoAdListener listener) {
        videoAd = new FullVideoAd(adUnitId, listener);
        videoAd.load(context, GMAdConstant.VERTICAL);
        if (context instanceof FragmentActivity) {
            ((FragmentActivity) context).getLifecycle().addObserver(new DefaultLifecycleObserver() {
                @Override
                public void onDestroy(@NonNull LifecycleOwner owner) {
                    DefaultLifecycleObserver.super.onDestroy(owner);
                    if (videoAd != null) {
                        videoAd.onDestroy();
                        videoAd = null;
                    }
                    ((FragmentActivity) context).getLifecycle().removeObserver(this);
                }
            });
        }
    }

    private FullVideoAd(String adUnitId, VideoAdListener listener) {
        this.adUnitId = adUnitId;
        this.listener = listener;
    }

    private void load(Activity context, final int orientation) {
        mAdFullVideoManager = new AdFullVideoManager(context, new GMFullVideoAdLoadCallback() {

            @Override
            public void onFullVideoLoadFail(AdError adError) {
                mLoadSuccess = false;
                LogADUtils.e(TAG, "onFullVideoLoadFail....全屏加载失败！");
                mAdFullVideoManager.printLoadFailAdnInfo();// 获取本次waterfall加载中，加载失败的adn错误信息。
            }

            @Override
            public void onFullVideoAdLoad() {
                LogADUtils.d(TAG, "onFullVideoAdLoad....加载成功！");
                mLoadSuccess = true;
                mAdFullVideoManager.printLoadAdInfo(); //已经加载广告的信息
                mAdFullVideoManager.printLoadFailAdnInfo(); ////打印加载失败的adn错误信息
            }

            @Override
            public void onFullVideoCached() {
                mLoadSuccess = true;
                LogADUtils.d(TAG, "onFullVideoCached....缓存成功！");
                show(context);
            }
        });

        if (mAdFullVideoManager != null) {
            mAdFullVideoManager.loadAdWithCallback(adUnitId, orientation);
        }
    }

    /**
     * 展示广告
     */
    private void show(Activity activity) {
        if (mLoadSuccess && mAdFullVideoManager != null) {
            if (mAdFullVideoManager.getFullVideoAd() != null && mAdFullVideoManager.getFullVideoAd().isReady()) {
                //在获取到广告后展示,强烈建议在onRewardVideoCached回调后，展示广告，提升播放体验
                //该方法直接展示广告，如果展示失败了（如过期），会回调onVideoError()
                //展示广告，并传入广告展示的场景
                mAdFullVideoManager.getFullVideoAd().setFullVideoAdListener(mTTFullVideoAdListener);
                mAdFullVideoManager.getFullVideoAd().showFullAd(activity);
                mAdFullVideoManager.printSHowAdInfo(); //打印已经展示的广告信息
            } else {
                LogADUtils.d(TAG, "当前广告不满足show的条件");
            }
        } else {
            LogADUtils.d(TAG, "请先加载广告");
        }
    }

    private GMFullVideoAdListener mTTFullVideoAdListener = new GMFullVideoAdListener() {

        @Override
        public void onFullVideoAdShow() {
            if (listener != null) {
                listener.onStart();
            }
            mLoadSuccess = false;
            LogADUtils.d(TAG, "onFullVideoAdShow");
        }

        /**
         * show失败回调。如果show时发现无可用广告（比如广告过期或者isReady=false），会触发该回调。
         * 开发者应该结合自己的广告加载、展示流程，在该回调里进行重新加载。
         * @param adError showFail的具体原因
         */
        @Override
        public void onFullVideoAdShowFail(AdError adError) {
            mLoadSuccess = false;
            LogADUtils.d(TAG, "onFullVideoAdShowFail");
            // 开发者应该结合自己的广告加载、展示流程，在该回调里进行重新加载
        }

        @Override
        public void onFullVideoAdClick() {
            LogADUtils.d(TAG, "onFullVideoAdClick");
        }

        @Override
        public void onFullVideoAdClosed() {
            LogADUtils.d(TAG, "onFullVideoAdClosed");
        }

        @Override
        public void onVideoComplete() {
            LogADUtils.d(TAG, "onVideoComplete");
        }

        /**
         * 1、视频播放失败的回调 - Mintegral GDT Admob广告不存在该回调；
         */
        @Override
        public void onVideoError() {
            LogADUtils.d(TAG, "onVideoError");
        }

        @Override
        public void onSkippedVideo() {
            LogADUtils.d(TAG, "onSkippedVideo");
        }

        @Override
        public void onRewardVerify(@NonNull RewardItem rewardItem) {
            Map<String, Object> customData = rewardItem.getCustomData();
            if (customData != null) {
                String adnName = (String) customData.get(RewardItem.KEY_ADN_NAME);
                if (adnName != null) {
                    switch (adnName) {
                        case RewardItem.KEY_GDT:
                            Logger.d(TAG, "rewardItem gdt: " + customData.get(RewardItem.KEY_GDT_TRANS_ID));
                            break;
                    }
                }
            }
            LogADUtils.d(TAG, "onRewardVerify");
            if (listener != null) {
                listener.onComplete();
            }
        }
    };

    public void onDestroy() {
        if (mAdFullVideoManager != null) {
            mAdFullVideoManager.destroy();
        }
    }
}
